# Create your views here.
from django.shortcuts import render_to_response
from django.shortcuts import redirect
from django.core.context_processors import csrf

def badpasswords(request):

    content = {}
    content.update(csrf(request))

    if request.method == 'POST':
        if str(request.POST['pass']).lower() == "woofwoof" and str(request.POST['user']).lower() == 'lassie':
            content["message"] = "Login Successful... Welcome back Lassie..."
        elif str(request.POST['user']).lower() == 'admin':
            content["message"] = "Login Successful... Welcome back Admin..."
        elif str(request.POST['pass']).lower() == "jane" and str(request.POST['user']).lower() == 'jane':
            content["message"] = "Login Successful... Welcome back Jane..."
        elif str(request.POST['pass']).lower() == "qwerty" and str(request.POST['user']).lower() == 'john':
            content["message"] = "Login Successful... Welcome back John..."
        else:
            content['message'] = "Login Failure..."

    return render_to_response("Lab5/badpassword.html", content)

def policy(request):
    return render_to_response("Lab5/policy.html")

def noisymessages(request):

    content = {}
    content.update(csrf(request))

    users = ['Godzilla', 'Gigan', 'Ghidorah', 'Megalon', 'Gaira', 'Mechagodzilla', 'Phoenix', 'Orochi', 'Baragon']

    if request.method == 'POST':
        if str(request.POST['user']) in users:
            content['message'] = "Wrong password, " + request.POST['user']
        else:
            content['message'] = "Unknown user..."

    return render_to_response("Lab5/noisymessages.html", content)

def changepassword(request):

    content = {}
    content.update(csrf(request))

    users = ['Ace', 'Trucker', 'Applejack', 'Braeburn', 'Bubbles', 'Caramel', 'Cheerllee', 'Cheval', 'Cupcake', 'Sugarcup', 'Daffidazey', 'Ember']

    if request.method == 'POST':
        if str(request.POST['user']) in users:
            if str(request.POST['oldpass']).lower == 'hasbro':
                content['message'] = "Password change successful..."
            else:
                content['message'] = "Sorry " + request.POST['user'] + " your password is wrong..."
        else:
            content['message'] = "Unknown User..."

    return render_to_response("Lab5/changepassword.html", content)

def rememberme1(request):

    content = {}
    content.update(csrf(request))

    response = render_to_response("Lab5/rememberme1.html", content)

    if request.method == 'POST':

        content['message'] = "Welcome " + request.POST['user']

        if request.POST['user'] == 'John' and request.POST['pass'] == 'qwerty':
            response = render_to_response("Lab5/rememberme1.html", content)
            response.set_cookie("user", "John")
    else:
        if 'user' in request.COOKIES.keys():
            content['message'] = "Welcome " + request.COOKIES.get('user')
            response = render_to_response("Lab5/rememberme1.html", content)

    return response

def rememberme2(request):

    content = {}
    content.update(csrf(request))

    response = render_to_response("Lab5/rememberme2.html", content)

    if request.method == 'POST':

        content['message'] = "Welcome " + request.POST['user']

        if request.POST['user'] == 'John' and request.POST['pass'] == 'qwerty':
            response = render_to_response("Lab5/rememberme2.html", content)
            response.set_cookie("user", "1597")
    else:
        if 'user' in request.COOKIES.keys():
            user = {}
            user['1597'] = "John"
            user['666'] = "Devil"
            user['1'] = "Admin"
            user['1776'] = "Washington"
            user['911'] = "George W. Bush"
            user['1941'] = "FDR"

            if request.COOKIES.get('user') not in user.keys():
                response.delete_cookie("user")
            else:
                content['message'] = "Welcome " + user[request.COOKIES.get('user')]

            response = render_to_response("Lab5/rememberme2.html", content)

    return response

def incompletevalidation(request):

    content = {}
    content.update(csrf(request))

    if request.method == 'POST':
        if str(request.POST['pass']).lower() == "UJ56stRZ".lower() and str(request.POST['user']).lower() == 'lassie':
            content["message"] = "Login Successful... Welcome back Lassie..."
        elif str(request.POST['pass'])[:4] == "4kW7fhPK"[:4] and str(request.POST['user']).lower() == 'admin':
            content["message"] = "Login Successful... Welcome back Admin..."
        elif ''.join(e for e in str(request.POST['pass']) if e.isalnum()) == "4D5PU49z" and str(request.POST['user']).lower() == 'jane':
            content["message"] = "Login Successful... Welcome back Jane..."
        elif str(request.POST['pass']).lower() == "Xub2X2Mr" and str(request.POST['user']).lower() == 'john':
            content["message"] = "Login Successful... Welcome back John..."
        else:
            content['message'] = "Login Failure..."

    return render_to_response("Lab5/incompletevalidation.html", content)

def failopen(request):
    content = {}
    content.update(csrf(request))

    if request.method == 'POST':
        try:
            username = request.POST['user']
            password = request.POST['pass']

            if username != "admin" and password != "Pass123":
                content['message'] = "Login failed..."
                return render_to_response("Lab5/failopen.html", content)
        except:
            pass

        content['message'] = "Login Successful..."

    return render_to_response("Lab5/failopen.html", content)

def multi1(request):

    content = {}
    content.update(csrf(request))

    if request.method == 'POST':
        response = redirect("/Lab5/multi2.html", content)
        response.cookies["user"] = request.POST['user']
        return response

    return render_to_response("Lab5/multi1.html", content)

def multi2(request):

    content = {}
    content.update(csrf(request))

    if request.method == 'POST':

        if request.COOKIES.get('user') == 'John' and request.POST['pass'] == 'qwerty':
            response = redirect("/Lab5/multi3.html", content)
            response.set_cookie("authenticated", "true")
            return response
        else:
           content['message'] = "Login failed..."
    else:
        return render_to_response("Lab5/multi2.html", content)

def multi3(request):
    content = {}
    content.update(csrf(request))

    if request.COOKIES.get("authenticated"):
        content['message'] = "Login Successful. Welcome in " + request.COOKIES.get("user")
        return render_to_response("Lab5/multi3.html", content)
    else:
        content['message'] = "Login failure..."
        return redirect("/Lab5/multi1.html", content)